﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class RoleUserController : MyControllerBase
    {
        RoleUserManager manager;
        RoleUserFetcher fetcher;
        public RoleUserController() : base()
        {
            this.manager = new RoleUserManager();
            this.fetcher = new RoleUserFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Create)]
        public AsmObject<RoleUserModel> Set(RoleUserModel roleUserModel)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("SET:[RQS|{1}]{0}", Util.ConvertToJson(roleUserModel), RequestIp));
            if (roleUserModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType().Name, String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmObject<RoleUserModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType().Name, String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
            }
            var result = manager.Set(roleUserModel);
            LogHelper.Debug(this.GetType().Name, String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( result.Result);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Delete)]
        public AsmObject<bool> Del(RoleUserModel roleUserModel)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(roleUserModel), RequestIp));
            if (roleUserModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType().Name, String.Format("DEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType().Name, String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
            }
            var result = manager.Delete(roleUserModel);
            LogHelper.Debug(this.GetType().Name, String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( result.Result);
            return result;
        }

        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Create)]
        public AsmArray<RoleUserModel> SetList(string jsonUserModels)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("SETLIST:[RQS|{1}]{0}", jsonUserModels, RequestIp));
            IEnumerable<RoleUserModel> roleUserModels = Util.ConvertArrayFromJson<RoleUserModel>(jsonUserModels);
            if (roleUserModels== null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType().Name, String.Format("SETLIST:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmArray<RoleUserModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType().Name, String.Format("SETLIST:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
            }
            var result = manager.SetList(roleUserModels);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType().Name, String.Format("SETLIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Delete)]
        public AsmObject<bool> DelList( string jsonUserModels)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("DELLIST:[RQS|{1}]{0}", jsonUserModels, RequestIp));
            IEnumerable<RoleUserModel> roleUserModels = Util.ConvertArrayFromJson<RoleUserModel>(jsonUserModels);
            if (roleUserModels == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType().Name, String.Format("DELLIST:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmObject<bool>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept, Data= false };
                LogHelper.Debug(this.GetType().Name, String.Format("DELLIST:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));

            }
            var result = manager.DeleteList(roleUserModels);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType().Name, String.Format("DELLIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }

        [HttpGet]
        [Authorize]
        public AsmArray<RoleUserModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            IList<RoleUserModel> domainList = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<RoleUserModel>() { Data = domainList, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType().Name, String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<RoleUserModel> ListByRole(int roleId)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("LISTBYROLE:[RQS|{1}]{0}", Util.ConvertToJson(roleId), RequestIp));
            int cnt = 0;
            IList<RoleUserModel> domainList = fetcher.ListByRole(roleId);
            DBHelper.Commit( true);
            var result = new AsmArray<RoleUserModel>() { Data = domainList, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType().Name, String.Format("LISTBYROLE:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmObject<RoleUserModel> Get(int did)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("GET:[RQS|{1}]{0}", Util.ConvertToJson(did), RequestIp));

            RoleUserModel roleUser = fetcher.Get(did);
            DBHelper.Commit( true);
            AsmObject<RoleUserModel> result = null;
            if (roleUser!=null)
                result=new AsmObject<RoleUserModel>() { Data = roleUser, Message = "操作成功", Result = AmsCode.Succ };
            else
               result= new AsmObject<RoleUserModel>() { Data = roleUser, Message = "无法找到数据", Result = AmsCode.DataNotFound };
            LogHelper.Debug(this.GetType().Name, String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}